package com.lucian.ge.auth.controller;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpEntity;


/**
 * Created by Lucicol on 2019/6/18.
 */

@Component
public class GetAuthController {

    private static String authenCenter;

    @Value("${com.authenCenter}")
    public void setAuthenCenter(String authenCenter) {
        this.authenCenter = authenCenter;
    }

    public static String getAuthenCenter() {
        return authenCenter;
    }

    /**
     * 管理员验证
     * @param token
     */

    public static String getAdminAuth(String token) {

        LinkedMultiValueMap params = new LinkedMultiValueMap();

        HttpHeaders headers = new HttpHeaders();
        headers.add("token", token);

        HttpEntity entity = new HttpEntity(params, headers);

        RestTemplate restTemplate = new RestTemplate();

        String result = restTemplate.postForObject(getAuthenCenter() + "/geweb/user/checkadmin" , entity , String.class);

        if (JSONObject.parseObject(result).getString("code").equals("301")) {
            return "您没有管理员权限访问！";
        }
        if (!JSONObject.parseObject(result).getString("code").equals("200")) {
            return JSONObject.parseObject(result).getString("message");
        }
        return "管理员验证通过";
    }

    /**
     * 普通注册用户验证
     * @param token
     */

    public static String getUserAuth(String token) {

        LinkedMultiValueMap params = new LinkedMultiValueMap();

        HttpHeaders headers = new HttpHeaders();
        headers.add("token", token);

        HttpEntity entity = new HttpEntity(params, headers);

        RestTemplate restTemplate = new RestTemplate();

        String result = restTemplate.postForObject(getAuthenCenter() + "/geweb/user/checkuser" , entity , String.class);

        if (JSONObject.parseObject(result).getString("code").equals("301")) {
            return "您没有普通注册用户权限访问！";
        }
        if (!JSONObject.parseObject(result).getString("code").equals("200")) {
            return JSONObject.parseObject(result).getString("message");
        }
        return "普通注册用户验证通过";
    }

    /**
     * 区长团长用户验证
     * @param token
     */

    public static String getPartnerAuth(String token) {

        LinkedMultiValueMap params = new LinkedMultiValueMap();

        HttpHeaders headers = new HttpHeaders();
        headers.add("token", token);

        HttpEntity entity = new HttpEntity(params, headers);

        RestTemplate restTemplate = new RestTemplate();

        String result = restTemplate.postForObject(getAuthenCenter() + "/geweb/user/checkpartner" , entity , String.class);

        if (JSONObject.parseObject(result).getString("code").equals("301")) {
            return "您没有合伙人用户权限访问！";
        }
        if (!JSONObject.parseObject(result).getString("code").equals("200")) {
            return JSONObject.parseObject(result).getString("message");
        }
        return "合伙人权限验证通过";
    }
}
